package com.dragon.flow.resource.de;

import com.dragon.flow.model.de.ProdDeModel;
import com.dragon.flow.service.de.ProdDeModelService;
import com.dragon.tools.common.ReturnCode;
import com.dragon.tools.pager.PagerModel;
import com.dragon.tools.pager.ParamVo;
import com.dragon.tools.vo.ReturnVo;
import org.flowable.bpmn.model.BpmnModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @title:
 * @author: min.wang
 * @since: 2024/6/30 16:04
 */
@RestController
@RequestMapping("/flow/de/prodDeModel")
public class ProdDeModelResource {

    @Autowired
    private ProdDeModelService prodDeModelService;

    /**
     * 分页获取列表
     *
     * @param params 查询参数
     * @return
     */
    @PostMapping(value = "/getPagerModel", produces = "application/json")
    public ReturnVo<PagerModel> getPagerModel(@RequestBody ParamVo<ProdDeModel> params) {
        ReturnVo<PagerModel> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        PagerModel<ProdDeModel> pm = prodDeModelService.queryPagerModel(params.getEntity(), params.getQuery());
        returnVo.setData(pm);
        return returnVo;
    }

    /**
     * 通过key来更新表达式名称
     * @param modelKey
     * @param processNameExp
     * @return
     */
    @PostMapping(value = "/updateProcessNameExpByKey", produces = "application/json")
    public ReturnVo<String> updateProcessNameExpByKey(@RequestParam String modelKey,@RequestParam String processNameExp) {
        ReturnVo<String> returnVo = new ReturnVo<>(ReturnCode.FAIL, "操作失败！");
        prodDeModelService.updateProcessNameExpByKey(modelKey, processNameExp);
        returnVo.setCode(ReturnCode.SUCCESS);
        returnVo.setMsg("操作成功");
        return returnVo;
    }

    /**
     * 判断modelKey是否存在
     *
     * @param modelKey
     * @return
     */
    @PostMapping(value = "/getDeModelCount/{modelKey}", produces = "application/json")
    public ReturnVo<Integer> getDeModelCount(@PathVariable String modelKey) {
        ReturnVo<Integer> returnVo = new ReturnVo<>(ReturnCode.FAIL, "操作失败！");
        Integer count = prodDeModelService.getDeModelCount( modelKey);
        returnVo.setData(count);
        returnVo.setCode(ReturnCode.SUCCESS);
        returnVo.setMsg("操作成功");
        return returnVo;
    }

    /**
     * 通过key获取模型
     *
     * @return
     */
    @PostMapping(value = "/getModelByKey", produces = "application/json")
    public ReturnVo<ProdDeModel> getModelByKey(@RequestParam Integer modelType,@RequestParam String modelKey) {
        ReturnVo<ProdDeModel> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        ProdDeModel getdDeModel = prodDeModelService.getModelByKey(modelType,modelKey);
        returnVo.setData(getdDeModel);
        return returnVo;
    }

    /**
     * 通过key获取模型
     *
     * @return
     */
    @PostMapping(value = "/getBpmnModelByKey/{key}", produces = "application/json")
    public  ReturnVo<ProdDeModel> getBpmnModelByKey(@PathVariable String key){
        ReturnVo<ProdDeModel> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        ProdDeModel getdDeModel = prodDeModelService.getBpmnModelByKey(key);
        returnVo.setData(getdDeModel);
        return returnVo;
    }

    /**
     * 验证modelkey
     *
     * @return
     */
    @PostMapping(value = "/validateModelKey", produces = "application/json")
    public ReturnVo<Boolean> validateModelKey(@RequestParam Integer modelType,@RequestParam  String modelKey){
        ReturnVo<Boolean> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        boolean flag = prodDeModelService.validateModelKey(modelType, modelKey);
        returnVo.setData(flag);
        return returnVo;
    }

    /**
     * 通过xml 获取bpmnmodel
     *
     * @return
     */
    @PostMapping(value = "/getBpmnModelByXml/{xml}", produces = "application/json")
    public  ReturnVo<BpmnModel> getBpmnModelByXml(@PathVariable String xml) {
        ReturnVo<BpmnModel> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        BpmnModel bpmnModel = prodDeModelService.getBpmnModelByXml(xml);
        returnVo.setData(bpmnModel);
        return returnVo;
    }

}
